// pages/index/mediationroom/contradictiondetails/contradictiondetails.js
import Dialog from "../../../../miniprogram_npm/@vant/weapp/dialog/dialog";
import Toast from "../../../../miniprogram_npm/@vant/weapp/toast/toast"

const app = getApp()
//获取 log 日志打印
const log = new app.appLogs()
//获取 http 网络请求
const http = new app.appHttp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        is_button: true,
        isLoading: true,
        seeMore: true,
        show: false,
        ContradictionList: [],
        id: "",
        text: null,
        popupheighe: 414,
        people: "",
        mediate_code: "",
        home_id: "",
        scrollheight: "",
        detail: 'msg-0', // 锚点id
        imageFile: null,
        setInter: null,
        ContradictionListSize: null,
        Contradiction: {},
        t_length: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let query = wx.createSelectorQuery()
        wx.getSystemInfo({
            success: res => {
                query.selectAll('.top').boundingClientRect(rect => {
                    let heightAll = 0;
                    rect.map((currentValue, index, arr) => {
                        heightAll = heightAll + currentValue.height
                    })
                    this.setData({
                        scrollheight: (res.windowHeight - heightAll) * 2 - 60
                    })
                }).exec();
            }
        })
        this.setData({
            people: app.globalData.userInfo.applet_user_id,
            id: options.id,
        })
        wx.setNavigationBarTitle({
            title: options.title
        })
        this.getContradictionDetails()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        clearInterval(this.data.setInter)

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        const than = this
        http.postJson("mediate_information_user/add", {
            name: wx.getStorageSync("name"),
            sex: app.globalData.userInfo.sex,
            type: "1",
            phone: wx.getStorageSync("phone"),
            mediate_information_id: than.data.id,
            applet_user_id: app.globalData.userInfo.applet_user_id,
            in_type: "4"
        }).then(res => {

        })
        clearInterval(this.data.setInter)
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    /**
     * 主题是否完全显示
     */
    onOpen: function () {
        this.setData({
            seeMore: !this.data.seeMore
        })
    },
    /**
     * 添加留言
     */
    onLeavingMessage: function (e) {

        this.setData({
            show: true
        });

    },
    /**
     *获取房间详情
     */
    getContradictionDetails: function () {
        const than = this
        http.get("mediate_information/getOne", {
            id: than.data.id
        }).then(res => {
            if (res.data.code == 200) {
                res.data.data.create_time = res.data.data.create_time.replace('T', ' ');
                than.setData({
                    home_id: res.data.data.id,
                    Contradiction: res.data.data,
                    mediate_code: res.data.data.mediate_code
                })
                if (this.data.setInter != null) {
                    clearInterval(this.data.setInter)
                }
                than.getContradictionList()
                if (res.data.data.status == 0) {
                    than.setData({
                        is_button: true
                    })
                } else if (res.data.data.status == 1) {
                    than.setData({
                        is_button: false
                    })
                }
            } else {
                Toast.fail('获取失败');
            }
        })
    },
    /**
     * 获取留言列表
     */
    getContradictionList: function () {
        const than = this
        this.setData({
            isLoading: true
        })
        than.data.setInter = setInterval(function () {
            http.get("mediate_information/getInforState", {
                id: than.data.home_id,
                page_size: 2000,
                page_number: 1,
                mediate_code: than.data.mediate_code,
            }).then(res => {
                if (res.data.code == 200) {
                    if (than.data.ContradictionListSize != res.data.data.count) {
                        than.setData({
                            ContradictionListSize: res.data.data.count,
                            ContradictionList: res.data.data.list,
                            detail: "msg-" + (res.data.data.count - 1)
                        })
                    }
                } else {
                    Toast.fail('获取失败');
                }
                this.setData({
                    isLoading: false
                })
            })
        }, 2000);
    },
    /**
     * 视频呼叫
     */
    onVideo: function (e) {
        const than = this
        const mediate_code = than.data.mediate_code
        const name = 'yilitang'
        http.postJson("mediate_information_user/add", {
            name: app.globalData.userInfo.nickname,
            sex: app.globalData.userInfo.sex,
            type: "1",
            phone: wx.getStorageSync("phone"),
            mediate_information_id: than.data.id,
            applet_user_id: app.globalData.userInfo.applet_user_id,
            in_type: "2"
        }).then(res => {
            wx.navigateTo({
                url: "/trtc/pages/room/room?roomID=" + mediate_code + "&template=grid&debugMode=false&localVideo=true&localAudio=true&enableEarMonitor=false&enableAutoFocus=true&localMirror=auto&enableAgc=true&enableAns=true&frontCamera=front&audioVolumeType=auto&audioQuality=high&videoWidth=360&videoHeight=640&userID=" + name + "&minBitrate=600&maxBitrate=900&encsmall=false&scene=rtc&cloudenv=PRO&enableBlackStream=0&streamID=&userDefineRecordID=&privateMapKey=&pureAudioMode=&recvMode=&enableRecvMessage=false"
            })
        })
    },
    /**
     * 放大图片
     * @param e
     */
    onImage: function (e) {
        const url = e.currentTarget.dataset.url
        const list = []
        list.push(url)
        wx.previewImage({
            current: url, // 当前显示图片的http链接
            urls: list // 需要预览的图片http链接列表
        })
    },
    /**
     * 关闭弹出层
     */
    onClose() {
        this.setData({show: false});
    },
    /**
     * 剩余多少字
     * @param e
     */
    bindText: function (e) {
        const t_text = e.detail.value.length;
        const text = e.detail.value
        this.setData({
            t_length: t_text,
            text: text
        })
    },
    /**
     * 发送消息
     * @param e
     */
    onMessage: function (e) {
        const than = this
        const image = []
        if (than.data.imageFile != null) {
            image.push(than.data.imageFile)
        }
        if ((image == [] || image.length <= 0) && (than.data.text == null || than.data.text == "")) {
            Toast.fail("不能发送的内容为空！");
            return
        }
        http.postJson("information_reply/add", {
            post_id: than.data.mediate_code,
            reply_user_id: app.globalData.userInfo.applet_user_id,
            reply_content: than.data.text,
            enclosure: image,
            phone: wx.getStorageSync("phone"),
            reply_user_name: wx.getStorageSync("name"),
        }).then(res => {
            if (res.data.code == 200) {
                than.setData({
                    show: false,
                    imageFile: null,
                    text: "",
                    t_length: 0
                });
                clearInterval(this.data.setInter)
                than.getContradictionList()
            } else {
                Toast.clear()
                Toast.fail(res.data.msg);
            }
        })
    },
    /**
     * 上传图片
     * @param e
     */
    upFile: function (e) {
        const than = this
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                wx.showLoading({
                    title: '上传图片中，请稍等……',
                })
                const tempFilePaths = res.tempFilePaths[0]
                http.upFile(tempFilePaths, "MediationRoom")
                    .then(res => {
                        than.setData({
                            imageFile: res.data.url
                        })
                        wx.hideLoading()
                    })
            }
        })
    },
    /**
     * 刷新
     * @param e
     */
    onRefreshText: function (e) {
        clearInterval(this.data.setInter)
        this.getContradictionList()
    },

    /**
     * 下拉触顶刷新
     * @param e
     */
    onRefresh: function (e) {
        Toast.fail("没有更多数据了");
    },
    /**
     * 获取聚焦
     */
    focus: function (e) {
        let than = this;
        log.e(than.data.popupheighe)
        than.setData({
            popupheighe: than.data.popupheighe + e.detail.height + 40
        })
        log.e(than.data.popupheighe)
    },
    /**
     *  失去聚焦(软键盘消失)
     */
    blur: function (e) {
        const than = this
        than.setData({
            popupheighe: 414
        })
    },
})